<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controllers;

use yii\web\Controller;
use app\models\GkyQyhSpStatement;
use app\api\Response;
use app\api\ApprovalMessage;

/**
 * Description of LeaveController
 *
 * @author yuilin
 */
class StatementController extends Controller {

    public $enableCsrfValidation = false;

    //put your code here
    public function actionCreate($userId) {
        $postData = \Yii::$app->request->post();
        $model = new GkyQyhSpStatement();
        $model->approver_id = isset($postData['approvaler']) ? $postData['approvaler'] : 1;
        $model->proposer_id = $userId;
        $model->responsible = isset($postData['responsible']) ? $postData['responsible'] : '';
        $model->correlation1 = isset($postData['correlation1']) ? $postData['correlation1'] : '';
        $model->correlation2 = isset($postData['correlation2']) ? $postData['correlation2'] : '';
        $model->create_time = date('Y-m-d H:i:s', time());
        $model->print_code = 'STAT' . rand(1000, 9999) . time();
        $model->status = 0;
        if ($model->save()) {
            Response::setHeader();
            echo 100;
            ApprovalMessage::sendMessage($model->proposer_id, $model->approver_id, 0, '说明书');

        } else {
            Response::setHeader();
            echo -100;
        }
    }

    public function actionGetStatement($printCode) {
        $result = GkyQyhSpStatement::getStatement($printCode);
        Response::setHeader();
        echo json_encode($result);
    }

    public function actionAgree($printCode,$opinion) {
        if (GkyQyhSpStatement::agreeStatement($printCode,$opinion)) {
            Response::setHeader();
            echo 1;
            $model = GkyQyhSpStatement::findOne(['print_code' => $printCode]);
            if (\Yii::$app->request->isGet) {
                ApprovalMessage::sendMessage($model->approver_id, $model->proposer_id, 1, '声明表', $printCode);
            }
        } else {
            Response::setHeader();
            echo -1;
        }
    }

    public function actionRefuse($printCode,$opinion) {
        if (GkyQyhSpStatement::refuseStatement($printCode,$opinion)) {
            Response::setHeader();
            echo 1;
            $model = GkyQyhSpStatement::findOne(['print_code' => $printCode]);
            if (\Yii::$app->request->isGet) {
                ApprovalMessage::sendMessage($model->approver_id, $model->proposer_id, -1, '声明表');
            }
        } else {
            Response::setHeader();
            echo -1;
        }
    }

}
